package com.woniuxy01;

import java.io.*;

public class Test07 {
    public static void main(String[] args) throws IOException {
        String source="D:\\aaa\\a.pdf";
        String target="D:\\aaa\\b.pdf";
        //获取当前时间的毫秒数，从1970-1-1 开始计时
        long start=System.currentTimeMillis();
        copyFileWithBytesBuffer(source,target);
        long end=System.currentTimeMillis();
        System.out.println(end-start);
    }

    private static void copyFileWithBytesBuffer(String source, String target) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target));
        byte[] bytes=new byte[1024];
        int length;
        while ((length=bis.read(bytes))!=-1){
            bos.write(bytes,0,length);
        }
        bos.close();
        bis.close();
    }

    private static void copyFileWithByteBuffer(String source, String target) throws IOException {
//        FileInputStream fis = new FileInputStream(source);
//        FileOutputStream fos = new FileOutputStream(target);
        //创建带缓冲区的字节输入流
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target));
        int mybyte;
        while ((mybyte=bis.read())!=-1){
            bos.write(mybyte);
        }
        bos.close();
        bis.close();
    }


    private static void copyFileWithByte(String source, String target) throws IOException {
        FileInputStream fis = new FileInputStream(source);
        FileOutputStream fos = new FileOutputStream(target);
        int mybyte;
        while ((mybyte=fis.read())!=-1){
            fos.write(mybyte);
        }
        fos.close();
        fis.close();
    }
    private static void copyFileWithBytes(String source, String target) throws IOException {
        FileInputStream fis = new FileInputStream(source);
        FileOutputStream fos = new FileOutputStream(target);
        byte[] bytes=new byte[1024];
        int length;
        while ((length=fis.read(bytes))!=-1){
            fos.write(bytes,0,length);
        }
        fos.close();
        fis.close();
    }
    private static void copyFileWithBytesxxx(String source, String target) {
        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            fis = new FileInputStream(source);
            fos = new FileOutputStream(target);
            byte[] bytes=new byte[1024];
            int length;
            while ((length=fis.read(bytes))!=-1){
                fos.write(bytes,0,length);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if(fos!=null){
                    fos.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }
}
